package com.changgou.service.goods.service.impl;

import com.changgou.common.pojo.QueryCriteria;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.dao.BrandMapper;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;


    /**
     * 品牌列表查询
     *
     * @return
     */
    @Override
    public List<Brand> findAll() {
        //int i=3/0;
        return brandMapper.selectAll();
    }


    /**
     * 根据id查询品牌信息
     * @param id
     * @return
     */


    /**
     * 品牌新增
     *
     * @param brand
     */
    @Override
    public void add(Brand brand) {

        brandMapper.insert(brand);
    }

    /**
     * 品牌修改
     *
     * @param brand
     */
    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    /**
     * 根据id删除品牌信息
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }



    /**
     * 品牌列表条件查询
     * @param
     * @return
     */


/**
 * 分页查询*/
    @Override
    public Page<Brand> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<Brand>) brandMapper.selectAll();
    }

    /**/
    @Override
    public Page<Brand> findPageAndQuery(QueryCriteria queryCriteria, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();

        if (queryCriteria.getQueryName()!=null){
            criteria.andLike("name","%"+queryCriteria.getQueryName()+"%");
        }
        if (queryCriteria.getLetter()!=null){
        criteria.andEqualTo("letter",queryCriteria.getLetter());
        }

        return (Page<Brand>) brandMapper.selectByExample(example);
    }
}
